<template>
  <div>
    <el-table
      v-loading="loading"
      :data="listData"
      style="width: 100%">
      <el-table-column
        align="center"
        prop="number"
        label="试题编号"
        width="120">
      </el-table-column>

      <el-table-column
        align="center"
        prop="subject"
        label="学科"
        width="120">
      </el-table-column>

      <el-table-column
        align="center"
        prop="catalog"
        label="目录"
        width="120">
      </el-table-column>

      <el-table-column
        align="center"
        label="题型"
        width="100">
        <template #default="{row}">
          <span v-if="row.questionType === '1'">单选</span>
          <span v-else-if="row.questionType === '2'">多选</span>
          <span v-else>简答</span>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="题干"
        width="200">
        <template #default="{row}">
          <div v-html="row.question"></div>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="录入时间"
        width="100">
        <template #default="{row}">
          <span>{{ row.createTime | timeTrans }}</span>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        label="难度"
        width="120">
        <template #default="{row}">
          <span v-if="row.difficulty === '1'">简单</span>
          <span v-else-if="row.difficulty === '2'">一般</span>
          <span v-else>困难</span>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        prop="creator"
        label="录入人"
        width="120">
      </el-table-column>

      <el-table-column
        align="center"
        label="审核状态"
        width="120">
        <template #default="{row}">
          <span v-if="row.chkState === 0">待审核</span>
          <span v-else-if="row.chkState === 1">已审核</span>
          <span v-else>已拒绝</span>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        prop="chkRemarks"
        label="审核意见"
        width="120">
      </el-table-column>

      <el-table-column
        align="center"
        prop="chkUser"
        label="审核人"
        width="120">
      </el-table-column>

      <el-table-column
        align="center"
        label="发布状态"
        width="120">
        <template #default="{row}">
          <span v-if="row.publishState === 1 && row.chkState === 1">已发布</span>
          <span v-if="row.publishState === 0 && row.chkState === 1">已下架</span>
          <span v-else>待发布</span>
        </template>
      </el-table-column>

      <el-table-column align="center" label="操作" fixed="right" width="250px">
        <template #default="{row}">
          <el-button type="text" @click="previewQuest(row.id)">预览</el-button>
          <el-button type="text" :disabled="row.chkState!==0" @click="updateState(row.id)">审核</el-button>
          <el-button type="text" :disabled="row.publishState===1" @click="updateQuest(row.id)">修改</el-button>
          <el-button type="text" @click="updatePubState(row)">
            <span v-if="row.publishState === 0">上架</span>
            <span v-else>下架</span>
          </el-button>
          <el-button type="text" @click="delItem(row.id)" :disabled="row.publishState===1">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { delBaseList, updatePublishState } from '@/api/querstionbank'

export default {
  name: 'ListTable',
  props: {
    loading: Boolean,
    listData: Array
  },
  data() {
    return {

    }
  },

  methods: {
    // 删除基础题目
    delItem(id) {
      this.$confirm('此操作将永久删除该文章, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          await delBaseList(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.$emit('success')
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 更新上架状态
    async updatePubState(row){
      const state = (row.publishState === 1 ? 0 : 1)
      await updatePublishState(row.id, state)
      this.$emit('success')
      this.getList(this.params)
    },
    // 审核
    async updateState(id) {
      this.$emit('openDialog', id)
    },
    updateQuest(id){
      this.$router.push({
        path: '/questionbank/TestEntry',
        query: { id: id }
      })
    },
    previewQuest(id) {
      this.$emit('previewQuest', id)
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
